package com.school.service.impl;

import com.school.common.Constants;
import com.school.common.ServiceResultEnum;
import com.school.common.TeaShopCategoryLevelEnum;
import com.school.controller.vo.IndexCategoryVO;
import com.school.controller.vo.SecondLevelCategoryVO;
import com.school.controller.vo.ThirdLevelCategoryVO;
import com.school.entity.GoodsCategory;
import com.school.entity.IndexConfig;
import com.school.mapper.GoodsCategoryMapper;
import com.school.service.GoodsCategoryService;
import com.school.util.BeanUtil;
import com.school.util.PageQueryUtil;
import com.school.util.PageResult;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService {
    @Resource
    private GoodsCategoryMapper goodsCategoryMapper;
    @Override
    public PageResult getCategorysPage(PageQueryUtil pageQueryUtil) {
        List<GoodsCategory> list = goodsCategoryMapper.findGoodsCategoryList(pageQueryUtil);
        int totalCount = goodsCategoryMapper.getTotalGoodsCategoryCount(pageQueryUtil);
        return new PageResult(list, totalCount, pageQueryUtil.getLimit(), pageQueryUtil.getPage());
    }

    @Override
    public String saveGoodsCategory(GoodsCategory goodsCategory) {
        GoodsCategory goodsCategory1=goodsCategoryMapper.selectByCategoryId(goodsCategory.getCategoryId(),goodsCategory.getCategoryLevel());
        if(goodsCategory1!=null){
            return ServiceResultEnum.SAME_INDEX_CONFIG_EXIST.getResult();
        }
        int count=goodsCategoryMapper.insertSelective(goodsCategory);
        if(count>0){
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String updateGoodsCategory(GoodsCategory goodsCategory) {
//        IndexConfig temp=goodsCategoryMapper.selectByPrimaryKey(goodsCategory.getCategoryId());
//        if(temp==null){
//            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
//        }
//        IndexConfig temp2=goodsCategoryMapper.selectByGoodIdAndType(goodsCategory.getGoodsId(),indexConfig.getConfigType());
//        if(temp2!=null&&temp2.getConfigId().longValue()!=indexConfig.getConfigId().longValue()){
//            return ServiceResultEnum.SAME_INDEX_CONFIG_EXIST.getResult();
//        }
        goodsCategory.setUpdateTime(new Date());
        if(goodsCategoryMapper.updateByPrimaryKeySelective(goodsCategory)>0){
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String deleteBatch(Integer[] ids) {
        if (ids.length<1){
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        int count = goodsCategoryMapper.deleteBatch(ids);
        if (count>0){
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }
    @Override
    public List<GoodsCategory> selectByLevelAndParentIdAndNumber(List<Long> parentIds, int categoryLevel) {
        return goodsCategoryMapper.selectByLevelAndParentIdAndNumber(parentIds, categoryLevel,0);
    }
    @Override
    public GoodsCategory getGoodsCategoryById(Long categoryId) {
        return goodsCategoryMapper.selectById(categoryId);
    }
    @Override
    public List<IndexCategoryVO> getCayegoriesForIndex() {
        List<IndexCategoryVO> indexCategoryVOS = new ArrayList<>();
        List <GoodsCategory> firstLevelCategories = goodsCategoryMapper.selectByLevelAndParentIdAndNumber(Collections.singletonList(0L), TeaShopCategoryLevelEnum.LEVEL_ONE.getLevel(), Constants.INDEX_CATEGORY_NUMBER);
        if (firstLevelCategories != null){
            ArrayList<Long> firstLevelCategoryIds = new ArrayList<>();
            for (GoodsCategory category : firstLevelCategories){
                firstLevelCategoryIds.add(category.getCategoryId());
            }
            List<GoodsCategory> secondLevelCategories = goodsCategoryMapper.selectByLevelAndParentIdAndNumber(firstLevelCategoryIds, TeaShopCategoryLevelEnum.LEVEL_TWO.getLevel(),0);
            if (!secondLevelCategories.isEmpty()){
                ArrayList<Long> secondLevelCategoryIds = new ArrayList<>();
                for (GoodsCategory category : secondLevelCategories){
                    secondLevelCategoryIds.add(category.getCategoryId());
                }
                List<GoodsCategory> thirdLevelCategories = goodsCategoryMapper.selectByLevelAndParentIdAndNumber(secondLevelCategoryIds, TeaShopCategoryLevelEnum.LEVEL_THREE.getLevel(), 0);
                if (thirdLevelCategories != null){
                    Map<Long, List<GoodsCategory>> thirdLevelCategoryMap = thirdLevelCategories.stream().collect(Collectors.groupingBy(GoodsCategory::getParentId));
                    List<SecondLevelCategoryVO> secondLevelCategoryVOS = new ArrayList<>();
                    for (GoodsCategory secondLevelCategory : secondLevelCategories){
                        SecondLevelCategoryVO secondLevelCategoryVO = new SecondLevelCategoryVO();
                        BeanUtils.copyProperties(secondLevelCategory, secondLevelCategoryVO);
                        if (thirdLevelCategoryMap.containsKey(secondLevelCategory.getCategoryId())){
                            List<GoodsCategory> tempGoodsCategoryVOS= thirdLevelCategoryMap.get(secondLevelCategory.getCategoryId());
                            secondLevelCategoryVO.setThirdLevelCategoryVOS(BeanUtil.copyList(tempGoodsCategoryVOS, ThirdLevelCategoryVO.class));
                            secondLevelCategoryVOS.add(secondLevelCategoryVO);
                        }
                    }
                    if (secondLevelCategoryVOS.size()>0){
                        Map<Long,List<SecondLevelCategoryVO>> secondLevelCategoryVOMap=secondLevelCategoryVOS.stream().collect(Collectors.groupingBy(SecondLevelCategoryVO::getParentId));
                        for (GoodsCategory firstCategory : firstLevelCategories){
                            IndexCategoryVO indexCategoryVO = new IndexCategoryVO();
                            BeanUtil.copyProperties(firstCategory,indexCategoryVO);
                            if (secondLevelCategoryVOMap.containsKey(firstCategory.getCategoryId())){
                                List<SecondLevelCategoryVO> tempGoodsCategoryVOS = secondLevelCategoryVOMap.get(firstCategory.getCategoryId());
                                indexCategoryVO.setSecondLevelCategoryVOS(tempGoodsCategoryVOS);
                                indexCategoryVOS.add(indexCategoryVO);
                            }
                        }
                    }
                }
            }
            return indexCategoryVOS;
        }
        return null;
    }
}
